package cn.itsource.ldj.user.service.impl;

import cn.itsource.ldj.base.domain.CodeTypeContants;
import cn.itsource.ldj.base.service.impl.BaseServiceImpl;
import cn.itsource.ldj.user.domain.LoginInfo;
import cn.itsource.ldj.user.domain.User;
import cn.itsource.ldj.user.domain.dto.UserDto;
import cn.itsource.ldj.user.mapper.LoginInfoMapper;
import cn.itsource.ldj.user.mapper.UserMapper;
import cn.itsource.ldj.user.service.IUserService;
import cn.itsource.ldj.util.AjaxResult;
import cn.itsource.ldj.util.MD5Utils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    @Transactional
    public void register(UserDto userDto) {
        // 验证验证码
        String code = userDto.getCode();
        // 取出redis中保存的验证码
        String codeAndTime = (String)redisTemplate.opsForValue().get(CodeTypeContants.REGISTER_CODE + ":" + userDto.getPhone());
        String redisCode = codeAndTime.split(":")[0];
        if (!code.equals(redisCode)) {
            throw new RuntimeException("验证码错误，请重新输入");
        }

        // 验证两次密码是否相同
        if (!userDto.getPassword().equals(userDto.getAffirmpassword())) {
            throw new RuntimeException("请确认密码");
        }

        // 根据userDto中的数据生成user
        User user = userDtoToUser(userDto);
        // 添加到数据库
        userMapper.save(user);
        // 将user对象存放在loginInfo中
        LoginInfo loginInfo = userToLoginInfo(user);
        // 将logininfo对象存放在数据库中
        loginInfoMapper.save(loginInfo);
    }

    /* 登录 */
    @Override
    public LoginInfo login(User user) {

        // 查询用户名是否正确
        LoginInfo loginInfo = loginInfoMapper.login(user.getUsername());
        if (loginInfo == null) {
            throw new RuntimeException("用户名错误");
        }

        // 查验密码是否正确
        String md5 = MD5Utils.encrypByMd5(user.getPassword() + loginInfo.getSalt());
        System.out.println(md5);
        System.out.println(loginInfo.getPassword());
        if (!md5.equals(loginInfo.getPassword())) {
            throw new RuntimeException("密码错误");
        }

        return loginInfo;
    }

    /* 将user对象数据移植到logininfo中 */
    private LoginInfo userToLoginInfo(User user) {

        LoginInfo loginInfo = new LoginInfo();
        // 设置是根据哪个表来登录
        loginInfo.setType(1);
        // 将user对象中的数据copy到logininfo对象中
        BeanUtils.copyProperties(user, loginInfo);
        // 将user对象的id设置到logininfo的uid中
        loginInfo.setUid(user.getId());
        return loginInfo;
    }

    /* 将userDto对象的数据移植到user中 */
    private User userDtoToUser(UserDto userDto) {

        User user = new User();
        // 将数据从userDto移植到user中
        user.setPhone(userDto.getPhone());

        // 盐值
        String salt = UUID.randomUUID().toString();
        user.setSalt(salt);
        // 设置用户默认值
        user.setState(1);
        // 加盐加密
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(password);

        return user;
    }
}
